package com.zrrd.jt_admin.manager.controller;

import com.zrrd.jt_admin.common.dto.LoginDto;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.ManagerLoginVo;
import com.zrrd.jt_admin.manager.service.ManagerService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author WanJl
 * @version 1.0
 * @title LoginController
 * @description
 * @create 2024/7/5
 */
@Api(tags = "管理员相关")    //指定在线文档中Controller的名字
@ApiResponses({
        @ApiResponse(code = 500, message = "服务器内部错误，请联系管理人员！"),
        @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
})
@RestController
@RequestMapping("/admin")
@ResponseBody
@Slf4j
public class LoginController {

    @Resource
    private ManagerService managerService;

    @Resource
    private RedisTemplate redisTemplate;

    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "当前接口是用来进行管理员登录的接口")
    public ResponseResult login(@RequestBody LoginDto loginDto) {
        if (StringUtils.isBlank(loginDto.getUsername()) || StringUtils.isBlank(loginDto.getPassword()))
            ResponseResult.defaultResponse(null,"","登录参数不合法");
        ManagerLoginVo managerLoginVo = managerService.login(loginDto.getUsername(), loginDto.getPassword());
        return ResponseResult.defaultResponse(managerLoginVo,"登录成功","登录失败，用户名或密码错误");
    }


    @PostMapping("/logout")
    @ApiOperation(value = "退出登录", notes = "当前接口是用来进行管理员退出登录的接口")
    public ResponseResult logout(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        String token = request.getHeader("token");
        log.info("前端传递过来的token——————》"+token);
        Boolean flag = redisTemplate.delete("TOKEN:" + token);
        return ResponseResult.defaultResponse(flag,"退出登录成功","请求发送失败");
    }
}
